package com.sctvcloud.zhaohua_sd.utils;

import com.jiongbull.jlog.JLog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理图片、
 */
public class ReNameUtil {

    private String path = "/storage/emulated/0/sc/11/";
    private static ReNameUtil instance;

    private ReNameUtil() {
    }

    public static synchronized ReNameUtil getInstance() {
        if (null == instance) {
            instance = new ReNameUtil();
        }
        return instance;
    }

    public void renameUtil() {
        List<String> city = new ArrayList<>();
        city.add("ab");
        city.add("bz");
        city.add("daz");
        city.add("dey");
        city.add("ganZ");
        city.add("guangAn");
        city.add("guangYuan");
        city.add("leS");
        city.add("liangShan");
        city.add("meishan");
        city.add("mianyang");
        city.add("nanchong");
        city.add("neijiang");
        city.add("panzhihua");
        city.add("suining");
        city.add("yaan");
        city.add("yibin");
        city.add("zigong");
        city.add("ziyang");

        List<String> nPath = new ArrayList<>();
        nPath.add("drawable-hdpi");
        nPath.add("drawable-xhdpi");
        nPath.add("drawable-xxhdpi");
        String fi = "";
        for (int i = 0; i < city.size(); i++) {
            for (int k = 0; k < nPath.size(); k++) {
                fi = path + city.get(i) + "/" + nPath.get(k) + "/";
                try {
                    List<String> files = getFileList(fi);
                    for (int s = 0; s < files.size(); s++) {
                        String oldName = files.get(s);
                        String newName = oldName.replace(" ", "");
                        newName = newName.replace("(", "");
                        newName = newName.replace(")", "");
                        if( rename(fi+oldName, fi+newName)){
                            JLog.e("ReNameUtil", "----success----"+oldName);
                        }else {
                            JLog.e("ReNameUtil", "----faild --- "+oldName);
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
    }

    public boolean rename(String old, String news) {
        File fileNew = new File(news);
        File file = new File(old);
        if (file.exists()) {
            if (file.renameTo(new File(news))) {
                JLog.e("", "");
                return true;
            }
        }
        return false;
    }

    public synchronized static List<String> getFileList(String path) throws Exception {
        List<String> items = new ArrayList<String>();
        File f = new File(path);
        File[] files = f.listFiles();
        if (null != files && files.length >= 0) {
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                items.add(file.getName());
            }
            System.out.println("服务器文件数" + files.length);
        }
        return items;
    }
}
